package cn.itcast.wushangxiantu.Fragment.jiaoyisuo;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.JiaoYiSuoZhuangbeiAdapter;
import cn.itcast.wushangxiantu.UserData.JiaoYisuo;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class JiaoYiSuoZhuangBei extends Fragment {
    private View view;
    private Context context;
    private TextView tvSousuo;
    private Spinner spXuanze;
    private ExpandableListView expand;
    JiaoYiSuoZhuangbeiAdapter jiaoYiSuoZhuangbeiAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_jiaoyisuo_zhuangbei, container, false);
        initView(view);
        return view;
    }

    @SuppressLint("ValidFragment")
    public JiaoYiSuoZhuangBei(Context context) {
        this.context = context;
    }

    private void initView(View view) {
        tvSousuo = (TextView) view.findViewById(R.id.tvSousuo);
        spXuanze = (Spinner) view.findViewById(R.id.spXuanze);
        expand = (ExpandableListView) view.findViewById(R.id.expand);

        tvSousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=spXuanze.getSelectedItemPosition()+2;
                if (spXuanze.getSelectedItemPosition()==4){
                    num=7;
                }
                getData(num);
            }
        });
    }

    private void getData(int num) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG  + "need=goumaiyulan"+"&region="+num , new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("jiaoyisuogoumai1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("jiaoyisuogoumai2", src);
                JiaoYisuo jiaoYisuo = null;
                try {
                    jiaoYisuo = new Gson().fromJson(src, JiaoYisuo.class);
                }catch (Exception e){

                }
                Message message = new Message();
                message.obj = jiaoYisuo;
                message.what=1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
          if (message.what==1){
              JiaoYisuo jiaoYisuo = (JiaoYisuo) message.obj;
              if (jiaoYisuo!=null){
                  if (jiaoYiSuoZhuangbeiAdapter==null){
                      jiaoYiSuoZhuangbeiAdapter=new JiaoYiSuoZhuangbeiAdapter(context);
                      jiaoYiSuoZhuangbeiAdapter.setJiaoYisuo(jiaoYisuo);
                      expand.setAdapter(jiaoYiSuoZhuangbeiAdapter);
                  }
                  jiaoYiSuoZhuangbeiAdapter.setJiaoYisuo(jiaoYisuo);
              }else {
                  ToastUtil.toast(context,"服务器坏掉了");
              }
          }
            return false;
        }
    });

}
